var mongoose = require('mongoose');

var date = new Date();
var ArticleSchema = new mongoose.Schema({
	cat_id:Number,
	title:String,
	content:String,
	post_user:Number,
	author:Number,
	status:Number,
	tags:String,
	intro:String,
	top_type:Number,
	allow_comment:Number,
	read_count:Number,
	comment_count:Number,
	create_time:{
		type:Number,
		default:date.getTime()
	},
	update_time:{
		type:Number,
		default:date.getTime()
	},
	post_time:{
		type:Number,
		default:date.getTime()
	}
});

ArticleSchema.pre('save',function(next){
	if(this.isNew){
		this.create_time = this.update_time = date.getTime();
	}else{
		this.update_time = date.getTime();
	}

	next();
});

ArticleSchema.statics = {
	fetch:function(cb){
		return this.find({}).sort('post_time');
		exec(cb);
	},

	findById:function(id,cb){
		return this.findOne({_id:id});
		exec(cb);
	}
};


module.exports = ArticleSchema;